package com.duoketech.service.web.product;
import com.duoketech.config.Constant;
import com.duoketech.model.deliveryconfig.DeliveryConfig;
import com.duoketech.service.AbstractService;
import com.duoketech.model.product.Product;
import com.duoketech.exception.ValidateException;
import com.duoketech.utils.PageUtil;
import com.duoketech.utils.search.EqualCondition;
import com.duoketech.utils.search.LikeCondition;
import com.duoketech.utils.search.QueryConstructor;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringLengthBetween;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import org.apache.commons.lang3.StringUtils;

public class ProductService extends AbstractService{
    public static ProductService ME = new ProductService();

    @Override
    public void validatorUpdate(Model<?> model, Kv kv) throws ValidateException {
        validatorAdd(model,kv);
    }

    @Override
    public void validatorAdd(Model<?> model, Kv kv) throws ValidateException {

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(model.getStr(Product.NAME),"名称项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Product.NAME),"名称项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Product.IS_POST),"是否包邮项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Product.IS_POST),"是否包邮项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Product.WEIGHT),"重量项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Product.WEIGHT),"重量项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Product.PRICE),"单价项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Product.PRICE),"单价项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Product.IS_SALE),"是否在售项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Product.IS_SALE),"是否在售项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Product.DESC),"描述项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Product.DESC),"描述项字符必须为1-50个字符",1,50));
//        validator.addRule(new StringMustBeRequired(model.getStr(Product.COVER),"封面图片项为必填信息"));
//        validator.addRule(new StringLengthBetween(model.getStr(Product.COVER),"封面图片项字符必须为1-50个字符",1,50));
//        validator.addRule(new StringMustBeRequired(model.getStr(Product.IMGS),"图片集项为必填信息"));
//        validator.addRule(new StringLengthBetween(model.getStr(Product.IMGS),"图片集项字符必须为1-50个字符",1,50));
        validator.excute();
//        int isPost = model.getInt(Product.IS_POST);
//        if(isPost==Product.POST_NO){
//            model.set(Product.DELIVERY_TYPE,model.getInt(Product.DELIVERY_TYPE));
//        }else{
//            model.set(Product.DELIVERY_TYPE,-1);//假如包邮，则不设置快递编码
//        }
//        int weight=PageUtil.kg2G(model.getDouble(Product.WEIGHT));
//        int price=PageUtil.yuanToFeng(model.getDouble(Product.PRICE));
//        model.set(Product.WEIGHT,weight);
//        model.set(Product.PRICE,price);
    }

    @Override
    public void beforAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public String tableName() {
        return Product.dao.tableName();
    }

    @Override
    public void builderSQLExpected(Model<?> model, QueryConstructor queryConstruct, Kv kv) {
        queryConstruct.and(new EqualCondition(Product.STATUS,Product.STATUS_NORMAL));
        queryConstruct.and(new EqualCondition(Product.MEMBER_ID,kv.getInt(Constant.MEMBER_ID_KEY)));
        String value = model.getStr(Product.NAME);
        if(StringUtils.isNotEmpty(value)){
            queryConstruct.and(new LikeCondition(Product.NAME,"%"+value+"%"));
        }
        		
    }


    public Product queryProductBy(int id){
        Product product= Product.dao.findById(id);
        product.set(Product.PRICE,product.price());
        product.set(Product.WEIGHT,product.weight());
        product.set(Product.IS_POST,product.is_post());
        return product;
    }
}